<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" @click="handleSave">保存</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" v-if="flag">导出</a-button>
        <a-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls" v-if="flag">导入</a-button>
        <a-dropdown v-if="flag">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.editRow && column.dataIndex === 'workScore'">
          <a-input-number v-model:value="record[column['dataIndex']]" addon-after="分" :min="0" />
        </template>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <HubTeamWorkSyncSetModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="emp-team-base-HubTeamWorkSyncSet1" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import HubTeamWorkSyncSetModal from './components/HubTeamWorkSyncSetModal.vue';
  import { columns, searchFormSchema } from './HubTeamWorkSyncSet.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, updateList } from './HubTeamWorkSyncSet.api';
  // import { downloadFile } from '/@/utils/common/renderUtils';
  // 导入导出的显示与隐藏
  const flag = ref<boolean>(false);
  const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'hub_team_work_score_config',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, { workTypeCode: 'A', orgCode: pageParams.value.orgCode });
      }, //表格和查询条件同步更新的方法
      immediate: true,
      showIndexColumn: true,
    },
    exportConfig: {
      name: 'hub_team_work_score_config',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload, getForm, getDataSource, setTableData }, { selectedRowKeys }] = tableContext;

  /**
   * 保存事件
   */
  async function handleAdd() {
    // let data = getDataSource();
    // const list = {
    //   datas: data,
    //   orgCode: orgCodeBase.value,
    //   workTypeCode: 'A',
    // };
    openModal(true, {
      isUpdate: false,
      showFooter: true,
      record: {
        orgCode: getForm().getFieldsValue().orgCode,
        orgName: '123',
      },
    });
    // await updateList(list); // 更新数据
    // handleSuccess(); // 保存数据
  }
  async function handleSave() {
    let data = getDataSource();
    const list = {
      datas: data,
      orgCode: getForm().getFieldsValue().orgCode,
      workTypeCode: 'A',
    };
    await updateList(list);
    handleSuccess();
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    reload({
      searchInfo: {
        orgCode: getForm().getFieldsValue().orgCode,
        workTypeCode: 'A',
      },
    });
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  // function getDropDownAction(record) {
  //   return [
  //     {
  //       label: '详情',
  //       onClick: handleDetail.bind(null, record),
  //     },
  //     {
  //       label: '删除',
  //       popConfirm: {
  //         title: '是否确认删除',
  //         confirm: handleDelete.bind(null, record),
  //       },
  //     },
  //   ];
  // }
</script>

<style scoped></style>
